﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using DTO_MechSys;
using System.Data;
using System.IO;
using System.Drawing;

namespace DAL_MechSys
{
    public class DALPropaganda : DALBase
    {

        public void Excluir(int PropagandaID)
        {
            try
            {
                string storedProssidere = "sp_ExcluirPropaganda";
                SqlParameter[] parameters =
                        {                    
                           new SqlParameter("@PropagandaID", PropagandaID)                                    
                        };
                ExecuteNonQuery(storedProssidere, parameters);

            }

            catch (SqlException)
            {
                throw;
            }
        }

        public void Inserir(DTOPropaganda dtoPropaganda)
        {
            try
            {
                string storedProssidere = "sp_InserirPropaganda";
                SqlParameter[] parameters =
                
                { 
                   new SqlParameter("@altura", dtoPropaganda.Altura),
                   new SqlParameter("@largura", dtoPropaganda.Largura),
                   new SqlParameter("@margem", dtoPropaganda.Margem),
                   new SqlParameter("@imagem", dtoPropaganda.ImagemArray),
                   new SqlParameter("@nome", dtoPropaganda.Nome)

                };
                ExecuteNonQuery(storedProssidere, parameters);

            }

            catch (SqlException)
            {
                throw;
            }
        }

        public void Atualizar(DTOPropaganda dtoPropaganda)
        {
            try
            {
                string storedProssidere = "sp_AtualizarPropaganda";
                SqlParameter[] parameters =
                
                { 
                   new SqlParameter("@altura", dtoPropaganda.Altura),
                   new SqlParameter("@largura", dtoPropaganda.Largura),
                   new SqlParameter("@margem", dtoPropaganda.Margem),
                   new SqlParameter("@imagem", dtoPropaganda.ImagemArray),
                   new SqlParameter("@indice", dtoPropaganda.Indice),
                   new SqlParameter("@nome", dtoPropaganda.Nome),
                   new SqlParameter("@propagandaID", dtoPropaganda.PropagandaID)

                };
                ExecuteNonQuery(storedProssidere, parameters);

            }

            catch (SqlException)
            {
                throw;
            }
        }

        public void AlterarIndice(int PropagandaID1, int PropagandaID2)
        {
            try
            {
                string storedProssidere = "sp_AlterarIndicePropaganda";
                SqlParameter[] parameters =
                
                { 
                   new SqlParameter("@propagandaID1", PropagandaID1),
                   new SqlParameter("@propagandaID2", PropagandaID2)

                };
                ExecuteNonQuery(storedProssidere, parameters);

            }

            catch (SqlException)
            {
                throw;
            }
        }

        public List<DTOPropaganda> BuscarPropagandaCadastrados()
        {
            List<DTOPropaganda> lstPropaganda = new List<DTOPropaganda>();

            try
            {
                string storedProssidere = "sp_BuscarPropagandasCadastrados";
                using (SqlDataReader sqlDataReader = ExecuteReader(storedProssidere))
                {
                    while (sqlDataReader.Read())
                    {

                        DTOPropaganda dtoPropaganda = CriarPropaganda(sqlDataReader);

                        lstPropaganda.Add(dtoPropaganda);

                    }
                }
            }
            catch (SqlException)
            {
                throw;
            }
            return lstPropaganda;
        }

        public DTOPropaganda BuscarPropagandaID(int propagandaID)
        {
            DTOPropaganda dtoPropaganda = new DTOPropaganda();

            try
            {
                string storedProssidere = "sp_BuscarPropagandaPorID";
                SqlParameter[] parameters =
                { 
                   new SqlParameter("@propagandaID", propagandaID)
                };
                using (SqlDataReader sqlDataReader = ExecuteReader(storedProssidere, parameters))
                {
                    while (sqlDataReader.Read())
                    {
                        dtoPropaganda = CriarPropaganda(sqlDataReader);
                    }
                }
            }
            catch (SqlException)
            {
                throw;
            }
            return dtoPropaganda;
        }

        private DTOPropaganda CriarPropaganda(SqlDataReader sqlDataReader)
        {
            try
            {
                DTOPropaganda dtoPropaganda = new DTOPropaganda();


                if (!string.IsNullOrEmpty(sqlDataReader["Indice"].ToString()))
                {
                    dtoPropaganda.Indice = (int)sqlDataReader["Indice"];
                }

                if (!string.IsNullOrEmpty(sqlDataReader["Nome"].ToString()))
                {
                    dtoPropaganda.Nome = sqlDataReader["Nome"].ToString();
                }

                if (!string.IsNullOrEmpty(sqlDataReader["Largura"].ToString()))
                {
                    dtoPropaganda.Largura = (int)sqlDataReader["Largura"];
                }

                if (!string.IsNullOrEmpty(sqlDataReader["Altura"].ToString()))
                {
                    dtoPropaganda.Altura = (int)sqlDataReader["Altura"];
                }

                if (!string.IsNullOrEmpty(sqlDataReader["Margem"].ToString()))
                {
                    dtoPropaganda.Margem = (int)sqlDataReader["Margem"];
                }

                if (!string.IsNullOrEmpty(sqlDataReader["Imagem"].ToString()))
                {
                    byte[] aux = (byte[])sqlDataReader["Imagem"];
                    dtoPropaganda.Imagem = byteArrayToImage(aux);
                }

                if (!string.IsNullOrEmpty(sqlDataReader["PropagandaID"].ToString()))
                {
                    dtoPropaganda.PropagandaID = (int)sqlDataReader["PropagandaID"];
                }



                return dtoPropaganda;
            }
            catch (Exception)
            {
                throw;
            }
        }

        private Image byteArrayToImage(byte[] byteArrayIn)
        {
            MemoryStream ms = new MemoryStream(byteArrayIn);
            Image returnImage = Image.FromStream(ms);
            return returnImage;
        }
    }
}
